package com.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.dto.WorkflowProcessDTO;
import com.zhentao.pojo.WorkflowNode;
import com.zhentao.pojo.WorkflowProcess;
import com.zhentao.service.WorkflowNodeService;
import com.zhentao.service.WorkflowProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 流程定义Controller
 */
@RestController
@RequestMapping("/produce/workflow/process")
public class WorkflowProcessController {
    
    @Autowired
    private WorkflowProcessService workflowProcessService;
    
    @Autowired
    private WorkflowNodeService workflowNodeService;
    
    /**
     * 分页查询流程定义
     */
    @GetMapping("/list")
    public Map<String, Object> list(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String type) {
        
        try {
            Page<WorkflowProcess> page = new Page<>(current, size);
            QueryWrapper<WorkflowProcess> wrapper = new QueryWrapper<>();
            
            if (name != null && !name.isEmpty()) {
                wrapper.like("name", name);
            }
            if (type != null && !type.isEmpty()) {
                wrapper.eq("type", type);
            }
            
            wrapper.orderByDesc("create_time");
            Page<WorkflowProcess> result = workflowProcessService.page(page, wrapper);
            
            // 添加调试日志
            System.out.println("查询结果: " + result);
            System.out.println("总记录数: " + result.getTotal());
            System.out.println("当前页记录数: " + result.getRecords().size());
            
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "查询成功");
            response.put("data", result);
            
            return response;
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 500);
            response.put("message", "查询失败: " + e.getMessage());
            response.put("error", e.toString());
            return response;
        }
    }
    
    /**
     * 根据ID查询流程定义
     */
    @GetMapping("/getById/{id}")
    public Map<String, Object> getById(@PathVariable Long id) {
        WorkflowProcess process = workflowProcessService.getById(id);
        List<WorkflowNode> nodes = workflowNodeService.getNodesByProcessId(id);
        
        WorkflowProcessDTO dto = new WorkflowProcessDTO();
        dto.setId(process.getId());
        dto.setName(process.getName());
        dto.setType(process.getType());
        dto.setStatus(process.getStatus());
        dto.setRemark(process.getRemark());
        dto.setNodes(nodes);
        
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "查询成功");
        response.put("data", dto);
        
        return response;
    }
    
    /**
     * 新增流程定义
     */
    @PostMapping("/add")
    public Map<String, Object> add(@RequestBody WorkflowProcessDTO dto) {
        WorkflowProcess process = new WorkflowProcess();
        process.setName(dto.getName());
        process.setType(dto.getType());
        process.setStatus(dto.getStatus());
        process.setRemark(dto.getRemark());
        process.setCreateTime(new Date());
        process.setUpdateTime(new Date());
        
        boolean result = workflowProcessService.save(process);
        
        // 保存节点
        if (result && dto.getNodes() != null) {
            workflowNodeService.saveNodes(process.getId(), dto.getNodes());
        }
        
        Map<String, Object> response = new HashMap<>();
        if (result) {
            response.put("code", 200);
            response.put("message", "新增成功");
        } else {
            response.put("code", 500);
            response.put("message", "新增失败");
        }
        
        return response;
    }
    
    /**
     * 更新流程定义
     */
    @PutMapping("/update")
    public Map<String, Object> update(@RequestBody WorkflowProcessDTO dto) {
        WorkflowProcess process = new WorkflowProcess();
        process.setId(dto.getId());
        process.setName(dto.getName());
        process.setType(dto.getType());
        process.setStatus(dto.getStatus());
        process.setRemark(dto.getRemark());
        process.setUpdateTime(new Date());
        
        boolean result = workflowProcessService.updateById(process);
        
        // 更新节点
        if (result && dto.getNodes() != null) {
            workflowNodeService.saveNodes(process.getId(), dto.getNodes());
        }
        
        Map<String, Object> response = new HashMap<>();
        if (result) {
            response.put("code", 200);
            response.put("message", "更新成功");
        } else {
            response.put("code", 500);
            response.put("message", "更新失败");
        }
        
        return response;
    }
    
    /**
     * 删除流程定义
     */
    @DeleteMapping("/delete/{id}")
    public Map<String, Object> delete(@PathVariable Long id) {
        boolean result = workflowProcessService.removeById(id);
        
        Map<String, Object> response = new HashMap<>();
        if (result) {
            response.put("code", 200);
            response.put("message", "删除成功");
        } else {
            response.put("code", 500);
            response.put("message", "删除失败");
        }
        
        return response;
    }
    
    /**
     * 获取所有流程定义
     */
    @GetMapping("/getAll")
    public Map<String, Object> getAll() {
        List<WorkflowProcess> list = workflowProcessService.list();
        
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "查询成功");
        response.put("data", list);
        
        return response;
    }
} 